// pages/upgrade/progress/index.js
const gaiaModule = require('../../../utils/gaiaManager.js')
const gaiaManager = gaiaModule.gaiaManager

Page({
  data: {
    // 升级进度
    upgradeProgress: 0,
    upgradeStatus: '准备中...',
    upgradeState: 'idle',
    
    // 升级信息
    fileName: '',
    fileSize: 0,
    transferSpeed: 0,
    estimatedTime: 0,
    
    // 状态显示
    isCompleted: false,
    isFailed: false,
    isAborted: false
  },

  onLoad(options) {
    console.log('升级进度页面加载:', options)
    this.initPage()
  },

  onShow() {
    // 添加升级进度监听器
    this.addUpgradeListener()
  },

  onUnload() {
    // 移除升级进度监听器
    this.removeUpgradeListener()
  },

  // 初始化页面
  initPage() {
    // 获取当前升级状态
    if (gaiaManager.upgradeState) {
      this.setData({
        upgradeState: gaiaManager.upgradeState,
        upgradeProgress: gaiaManager.upgradeProgress || 0
      })
    }
  },

  // 添加升级进度监听器
  addUpgradeListener() {
    this.upgradeListener = (progressData) => {
      console.log('升级进度更新:', progressData)
      
      this.setData({
        upgradeProgress: progressData.progress,
        upgradeStatus: progressData.message,
        upgradeState: progressData.state
      })
      
      // 更新状态显示
      this.updateStatusDisplay(progressData.state)
    }
    
    gaiaManager.addUpgradeListener(this.upgradeListener)
  },

  // 移除升级进度监听器
  removeUpgradeListener() {
    if (this.upgradeListener) {
      gaiaManager.removeUpgradeListener(this.upgradeListener)
      this.upgradeListener = null
    }
  },

  // 更新状态显示
  updateStatusDisplay(state) {
    const statusMap = {
      'idle': { isCompleted: false, isFailed: false, isAborted: false },
      'initialisation': { isCompleted: false, isFailed: false, isAborted: false },
      'connecting': { isCompleted: false, isFailed: false, isAborted: false },
      'connected': { isCompleted: false, isFailed: false, isAborted: false },
      'transferring': { isCompleted: false, isFailed: false, isAborted: false },
      'completed': { isCompleted: true, isFailed: false, isAborted: false },
      'failed': { isCompleted: false, isFailed: true, isAborted: false },
      'aborted': { isCompleted: false, isFailed: false, isAborted: true }
    }
    
    const status = statusMap[state] || statusMap['idle']
    this.setData(status)
  },

  // 中止升级
  async onAbortUpgrade() {
    try {
      wx.showModal({
        title: '确认中止',
        content: '确定要中止升级吗？',
        success: async (res) => {
          if (res.confirm) {
            const result = await gaiaManager.abortUpgrade()
            if (result.success) {
              wx.showToast({
                title: '升级已中止',
                icon: 'success'
              })
            } else {
              wx.showToast({
                title: '中止失败: ' + result.error,
                icon: 'error'
              })
            }
          }
        }
      })
    } catch (error) {
      console.error('中止升级失败:', error)
      wx.showToast({
        title: '中止失败: ' + error.message,
        icon: 'error'
      })
    }
  },

  // 返回升级设置页面
  onBackToSettings() {
    wx.navigateBack()
  },

  // 重新开始升级
  onRestartUpgrade() {
    wx.navigateBack()
  }
})